﻿using IRepositories;
using iService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Repositories;
using Service;
using swiftCode.Bbs.Model;
using swiftCode.Bbs.Model.Model;

namespace swiftCode.Bbs.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class BanRecordController : ControllerBase
    {
        //private readonly IBanRecordService _banRecordService = new BanRecordService();
        //private readonly IUserService _userService = new UserService();

        private readonly IBanRecordService _banRecordService;
        private readonly IUserService _userService;

        public BanRecordController(IBanRecordService banRecordService, IUserService userService)
        {
            _banRecordService = banRecordService;
            _userService = userService;
        }



        /// <summary>
        /// 获取封禁名单
        /// </summary>
        /// <returns></returns>
        [HttpGet(Name = "getBanUser")]
        public async Task<MessageModel<List<BanRecord>>> getBanUser()
        {
            var Banuser = await _banRecordService.GetAllBanUsers();
            return new MessageModel<List<BanRecord>>()
            {
                success = Banuser.Count == 0 ? false : true,
                msg = Banuser.Count == 0 ? "未找到符合条件的内容" : "搜索成功",
                response = Banuser.Count == 0 ? null : Banuser
            };
        }


        /// <summary>
        /// 封禁账户
        /// </summary>
        /// <param name="adminId"></param>
        /// <param name="userId"></param>
        /// <param name="reason"></param>
        /// <param name="time"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        [HttpGet(Name = "addnewBan")]
        public async Task<MessageModel<String>> Ban(int adminId, int userId, string reason, int time)
        {
            User admin = await _userService.selectUser(d => d.UserId == adminId);
            BanRecord user = await _banRecordService.selectBanUser(d => d.UserId == userId);
            User user1 = await _userService.selectUser(d => d.UserId == userId);

            if (admin.Role == 2)
            {
                return new MessageModel<String>()
                {
                    success = false,
                    msg = admin.UserName + "不是管理员"
                };
            }else if(user != null)
            {
                return new MessageModel<String>()
                {
                    success = false,
                    msg = "该用户已经被封禁"
                };
            }else if(user1.Role == 1)
            {
                return new MessageModel<String>()
                {
                    success = false,
                    msg = "管理员不能被封禁"
                };
            }
            else
            {
                BanRecord newBan = new BanRecord();
                newBan.UserId = userId;
                newBan.AdminId = adminId;
                newBan.reason = reason;
                newBan.StartTime = DateTime.Now;
                newBan.EndTime = DateTime.Now.AddMinutes(time);
                await _banRecordService.AddBanUser(newBan, true);
                return new MessageModel<String>()
                {
                    success = true,
                    msg = "封禁成功"
                };
            }
        }


        /// <summary>
        /// 手动解封
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="confirm"></param>
        /// <returns></returns>
        [HttpGet(Name = "deleteBan")]
        public async Task<MessageModel<String>> DeleteBan(int userId)
        {
            BanRecord BanUser = await _banRecordService.selectBanUser(d => d.UserId==userId);

            await _banRecordService.DeleteBanUser(BanUser, true);
            return new MessageModel<String>()
            {
                success = true,
                msg = "删除成功"
            };
        }
    }
}
